<?php
/**
 * Ecga控制器
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Controllers;

use \Project\Apps\Ecga\Controllers\Abstraction\EcgaController;
use \Project\Apps\Ecga\Library\AdminMenu;
use \Project\Apps\Ecga\Library\Visitor;
use \Framework\Application\HttpApplication;
use \Framework\System\Helper\HttpResponse;

/**
 * Ecga控制器
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    v.k <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.1>
 **/
class Index extends EcgaController
{
    /**
     * index页只需要验证登录
     *
     * @param string $act 动作
     *
     * @author Garbin
     * @return void
     */
    public static function validate($act)
    {
        if (Visitor::getInstance()->logged()) {
            return true;
        } else {
            parent::validate($act);
        }
    }

    /**
     * 获取所有项目 并验证是不是罗技项目
     *
     * @return array
     */
    public static function getProjects()
    {
        $projectBool = false;
        $projects = model('userprojectprivs')->getProjectByUserId(Visitor::getInstance()->userId);
        if (count($projects) == 1 && 1 == $projects[0] && !Visitor::getInstance()->isBoss()) {
            $projectBool = true;
        } else {
            $projectBool = false;
        }
        return $projectBool;
    }
    /**
     * 默认Action
     *
     * @param HttpRequest $request request
     *
     * @return void
     */
    public static function show($request)
    {
        $page = self::createPage();
        $menus = self::_getMenus();

        //获取有权查看的第一个菜单
        $menuGroup = array_shift($menus);
        array_unshift($menus, $menuGroup);
        $defaultMenu = $menuGroup['items'][0]['controller'];

        $page->assign('appName', HttpApplication::getInstance()->appName);
        $page->assign('defaultMenu', $defaultMenu);
        $page->assign('menus', $menus);
        $page->assign('projectBool', self::getProjects());
        $page->assign('commonMenus', array());
        $page->assign('user', Visitor::getInstance());
        $page->setTitle('ECGA 平台');
        HttpResponse::output($page->render('index'));
    }

    /**
     * welcome
     *
     * @param HttpRequest $request request
     *
     * @return void
     */
    public static function welcome($request)
    {
        //...
    }

    /**
     * 取得菜单
     *
     * @return array array('name' => 菜单组名, 'items' => array(
     *                                              array('controller' => ?, 'act' => ?, 'name' => 菜单名),
     *                                              array('controller' => ?, 'act' => ?, 'name' => 菜单名),
     *                                          )),
     *               array('name' => 菜单组名, 'items' => array())
     * @author Scott Ye
     */
    private static function _getMenus()
    {
        $menus = AdminMenu::parseMenuFile();
        $visitor = Visitor::getInstance();

        /*过滤权限没有允许的菜单*/
        foreach ($menus as $key => $menu) {
            if (!$visitor->hasPrivilege($menu['controller'], $menu['action'])) {
                unset($menus[$key]);
            }
        }

        /* 格式化 */
        $formatedMenus = array();
        foreach ($menus as $menu) {
            $group = $menu['group'];
            if (isset($formatedMenus[$group])) {
                $formatedMenus[$group]['items'][] = array(
                    'controller' => $menu['controller'],
                    'act'        => $menu['action'],
                    'name'       => $menu['name']);
            } else {
                $formatedMenus[$group] = array(
                    'name'  => $group,
                    'items' => array(
                        array(
                            'controller' => $menu['controller'],
                            'act'        => $menu['action'],
                            'name'       => $menu['name'])));
            }
        }

        return $formatedMenus;
    }
}

